package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.enqualcomm.kids.R;

import java.util.Calendar;

import common.utils.DensityUtil;
import common.utils.MyFrontUtil;
import common.utils.MyLogger;

/**
 * Created by yinzhiqun on 2015/8/28.
 */
public class ShutDownDialog extends Dialog {

    private TextView time_tv;
    private boolean isOpen = true;
    private ImageView lineswitch;
    private String timeStr;
    private Listener listener;
    private ImageView select_auto_restart_iv;
    private ImageView no_select_auto_restart_iv;
    private boolean auto_restart = false;
    private String watchName;
    Context context;

    public ShutDownDialog(Context context, Listener listener) {
        super(context);
        this.listener = listener;
    }

    public ShutDownDialog(Context context, String watchName, Listener listener) {
        super(context);
        this.context = context;
        this.watchName = watchName;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_shutdown);
        setDialogAttributes();
        initDialogView();
    }

    private void setDialogAttributes() {
        Window dialogWindow = getWindow();
        setCanceledOnTouchOutside(false);
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    private void initDialogView() {
        time_tv = (TextView) findViewById(R.id.time_tv);

        int year, month, day, hour, minute;
        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeInMillis(System.currentTimeMillis() + 3600 * 24);
        long currentMinutes = calendar.getTimeInMillis();
        MyLogger.kLog().i("currentMinutes==============:" + currentMinutes);
        calendar.setTimeInMillis(currentMinutes + 3 * 60 * 1000);
        currentMinutes = calendar.getTimeInMillis();
        MyLogger.kLog().i("currentMinutes====change========:" + currentMinutes);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);

        StringBuilder builderForTV = new StringBuilder();
        StringBuilder builderForDialog = new StringBuilder();
        builderForTV.append(year);
        builderForDialog.append(year);
        appendTimeStr(builderForTV, builderForDialog, month);
        appendTimeStr(builderForTV, builderForDialog, day);
        appendHour(builderForTV, builderForDialog, hour);
        appendMinute(builderForTV, builderForDialog, minute);

        select_auto_restart_iv = (ImageView) findViewById(R.id.select_auto_restart_iv);
        no_select_auto_restart_iv = (ImageView) findViewById(R.id.no_select_auto_restart_iv);
        select_auto_restart_iv.setImageResource(R.drawable.mode_no_select);
        no_select_auto_restart_iv.setImageResource(R.drawable.mode_select);
        select_auto_restart_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                select_auto_restart_iv.setImageResource(R.drawable.mode_select);
                no_select_auto_restart_iv.setImageResource(R.drawable.mode_no_select);
                auto_restart = true;
            }
        });

        no_select_auto_restart_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                select_auto_restart_iv.setImageResource(R.drawable.mode_no_select);
                no_select_auto_restart_iv.setImageResource(R.drawable.mode_select);
                auto_restart = false;
            }
        });

        timeStr = builderForDialog.toString();
        time_tv.setText(builderForTV.toString());
        time_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new MyDateTimePickerDialog2(getContext(), new MyDateTimePickerDialog2.OnDateTimeSetListener() {
                    @Override
                    public void onDateTimeSet(String resultDateStr) {
                        timeStr = resultDateStr;
                    }
                }, timeStr, 3, time_tv).show();
            }
        });

        findViewById(R.id.sureBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (/*isOpen*/auto_restart) {
                    listener.schedulePower(time_tv.getText().toString());
                } else {
                    listener.justShutDown();
                }
                dismiss();
            }
        });

        findViewById(R.id.cancelBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        lineswitch = (ImageView) findViewById(R.id.lineswitch);
        lineswitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOpen) {
                    isOpen = false;
                    lineswitch.setImageResource(R.drawable.reai_alarm_close_icon);
                } else {
                    isOpen = true;
                    lineswitch.setImageResource(R.drawable.reai_alarm_open_icon);
                }
            }
        });

        if (!TextUtils.isEmpty(watchName)) {
            MyFrontUtil.initTextViewText(context, (TextView) findViewById(R.id.title_name_tv), watchName, context.getString(R.string.t_m_off_begin),
                    context.getString(R.string.t_m_off_end), 2, watchName.length() + 2);
        }

    }

    private void appendTimeStr(StringBuilder builderForTV, StringBuilder builderForDialog, int month) {
        if (month < 10) {
            builderForTV.append("-0").append(month);
            builderForDialog.append("0").append(month);
        } else {
            builderForTV.append("-").append(month);
            builderForDialog.append(month);
        }
    }

    private void appendHour(StringBuilder builderForTV, StringBuilder builderForDialog, int hour) {
        if (hour < 10) {
            builderForTV.append(" 0").append(hour);
            builderForDialog.append("0").append(hour);
        } else {
            builderForTV.append(" ").append(hour);
            builderForDialog.append(hour);
        }
    }

    private void appendMinute(StringBuilder builderForTV, StringBuilder builderForDialog, int hour) {
        if (hour < 10) {
            builderForTV.append(":0").append(hour);
            builderForDialog.append("0").append(hour);
        } else {
            builderForTV.append(":").append(hour);
            builderForDialog.append(hour);
        }
    }

    public interface Listener {
        void schedulePower(String timeStr);

        void justShutDown();
    }
}
